﻿/******************************************************************************
 *  Author：       Commscope ITDC Suzhou Web Team 
 *  CreateDate：   21/02/2012 2:29:49 PM
 *
 *
 ******************************************************************************/
using System;
using System.ComponentModel;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EasyDLL.Data;
using EasyDLL.Web.UI;

namespace EasyDLL.Web.AutoValidate
{
    /// <summary>
    /// form表单自动验证
    /// </summary>
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
    public class FormValidate : Panel
    {
        StringWrapper stringWrapper = new StringWrapper();
        public InValidateInfo ErrorInfo = new InValidateInfo(string.Empty, string.Empty, string.Empty);

        #region - 验证 -

        private bool ValidateControl(Control control)
        {
            //查看控件是否有ValidationGroup属性，没有则默认验证通过
            PropertyInfo propertyInfo = control.GetType().GetProperty("ValidationGroup");
            if (propertyInfo == null)
            {
                return true;
            }

            string validateGroupStr = propertyInfo.GetValue(control, null).ToString();


            string splitStr = "=>";
            string errorDesc = string.Empty;
            string validateRule = string.Empty;

            int containsCount = stringWrapper.GetContainsCount(validateGroupStr, splitStr);
            if (containsCount > 1)
            {
                throw new InvalidEnumArgumentException("控件" + control.ID + "的ValitionGroup属性中包括多个=>分隔标记");
            }
            if (containsCount == 0)
            {
                validateRule = validateGroupStr;
            }
            if (containsCount == 1)
            {
                validateRule = validateGroupStr.Substring(0, validateGroupStr.IndexOf(splitStr));
                errorDesc = validateGroupStr.Substring(validateGroupStr.IndexOf(splitStr) + splitStr.Length);
            }

            object value = ControlWrapper.GetControlValue(control);

            bool isPass = true;
            switch (validateRule.ToLower())
            {
                case "notnull":
                    isPass = DataValidateWrapper.IsNotNullOrEmpty(value);
                    break;

                case "mail":
                    isPass = DataValidateWrapper.IsEmail(Convert.ToString(value));
                    break;

                case "number":
                    isPass = DataValidateWrapper.IsNumber(value.ToString());
                    break;
            }

            if (!isPass)
            {
                ErrorInfo.ControlId = control.ID;
                ErrorInfo.ValidateRule = validateRule;
                ErrorInfo.ErrorDesc = errorDesc;
                return false;
            }
            return true;
        }

        public bool Validate()
        {
            //循环本Panel下的所有服务器控件，对于需要验证的控件得到对应的
            //客户端隐藏域从而得到验证规则
            foreach (Control control in Controls)
            {
                if (!ValidateControl(control)) return false;
            }
            return true;
        }

        public bool ValidateExcept(params Control[] exceptControls)
        {
            foreach (Control control in Controls)
            {
                foreach (var exceptControl in exceptControls)
                {
                    if (control != exceptControl)
                    {
                        if (!ValidateControl(control)) return false;
                    }
                }
            }
            return true;
        }

        public bool ValidateInclude(params Control[] includeControls)
        {
            foreach (Control control in Controls)
            {
                foreach (var includeControl in includeControls)
                {
                    if (control == includeControl)
                    {
                        if (!ValidateControl(control)) return false;
                    }
                }
            }
            return true;
        }

        #endregion


        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);

            foreach (Control control in Controls)
            {
                if (control is WebControl)
                {
                    (control as WebControl).Attributes.Add("datatype", "test");
                }
            }
        }
    }

    public class InValidateInfo
    {
        public string ControlId { get; set; }
        public string ValidateRule { get; set; }
        public string ErrorDesc { get; set; }

        public InValidateInfo(string controlId, string validateRule, string errorDesc)
        {
            ControlId = controlId;
            ValidateRule = validateRule;
            ErrorDesc = errorDesc;
        }
    }

    public enum ValidateRules
    {
        NotNull,
        EMail,
        Other
    }
}
